import requests
import time
ID = "ww7b4086d88a144eec"
SECRET = "QqZ9Au1EwEDc1PyvcZaAgJBhaPDeK1z5zinENKHLL1Q"
url_get_access_token = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + ID + "&corpsecret=" + SECRET
print(url_get_access_token)

#获取access_token#
def get_access_token(ID,SECRET):
    access_token_json = requests.get(url_get_access_token).json()
    while access_token_json['errcode'] != 0:
        get_access_token(ID, SECRET)
        time.sleep(2)
    access_token = access_token_json['access_token']
    return access_token

get_access_token(ID,SECRET)

#获取群发任务列表#
url_get_publish_list = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_groupmsg_list?access_token=" + get_access_token(ID, SECRET)

start_time = 1612627200
end_time = 1612713600
def get_publish_list_msgid(start_time, end_time):
    publish_inform = {
        "chat_type": "single",
        "start_time": start_time,
        "end_time": end_time,
        "creator": "201788",
        "filter_type": 0,
        "limit": 50,
        "cursor": ""
    }
    publish_list = requests.post(url_get_publish_list, json=publish_inform)
    return publish_list.json()['group_msg_list']

#获取满足条件的msgid列表#
msgid_list = []
for publish_content in get_publish_list_msgid(1612687200, 1612687260):
    msgid = publish_content['msgid']
    msgid_list.append(msgid)
print(msgid_list)


'''
#获取群发成员执行结果#
url_get_groupmsg_send_result = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_groupmsg_send_result?access_token=" + get_access_token(ID, SECRET)

def get_groupmsg_send_result():
    publish_inform = {
        "msgid": "msgWRjVEQAAZMpoU9t_tpQS8Vg6WpOu1Q",
        "userid": "201788 ",
        "limit": 50,
        "cursor": ""
    }
    groupmsg_send_result = requests.post(url_get_groupmsg_send_result, json=publish_inform)
    return groupmsg_send_result.json()['send_list']

print(get_groupmsg_send_result())
'''

#获取群发成员发送任务列表#
url_get_groupmsg_task = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_groupmsg_task?access_token=" + get_access_token(ID, SECRET)
'''
#.json()返回的是.dict字典格式#
print(type(requests.post(url_get_groupmsg_task, json={
            "msgid": "msgWRjVEQAAZMpoU9t_tpQS8Vg6WpOu1Q",
            "limit": 1000,
            "cursor": ""
        }).json()))
'''
#若按此行以下写，会覆盖#
import csv
def get_groupmsg_task(start_time, end_time):
    for publish_content in get_publish_list_msgid(start_time, end_time):
        msgid = publish_content['msgid']
        publish_inform = {
            "msgid": msgid,
            "limit": 10000,
            "cursor": ""
        }
        groupmsg_task_list_dict = requests.post(url_get_groupmsg_task, json=publish_inform).json()['task_list']
        for groupmsg_task_dict in groupmsg_task_list_dict:
            msgid_dict = {'msgid': msgid}
            groupmsg_task_dict.update(msgid_dict)
        '''
        print(groupmsg_task_list_dict)
        '''
        with open('test.csv', 'w', newline='')as f:
            f_csv = csv.DictWriter(f, ['userid', 'status', 'send_time', 'msgid'])
            f_csv.writeheader()
            f_csv.writerows(groupmsg_task_list_dict)
    return
get_groupmsg_task(1612680660, 1612686840)